package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import entity.Pet;
import dao.BaseDao;

public class PetDaoImpl implements PetDao {
    private BaseDao baseDao;

    public PetDaoImpl() {
        this.baseDao = new BaseDao();
    }

    @Override
    public List<Pet> findAll() {
        List<Pet> pets = new ArrayList<>();
        String sql = "SELECT * FROM pet";
        try (Connection conn = baseDao.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql);
             ResultSet rs = pstmt.executeQuery()) {
            while (rs.next()) {
                Pet pet = new Pet();
                pet.setPetId(rs.getLong("petId"));
                pet.setPetName(rs.getString("petName"));
                pet.setPetBreed(rs.getInt("petBreed"));
                pet.setPetSex(rs.getInt("petSex"));
                pet.setBirthday(rs.getTimestamp("birthday"));
                pet.setDescription(rs.getString("description"));
                pets.add(pet);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return pets;
    }

    @Override
    public List<Pet> findByBreed(Integer breed) {
        List<Pet> pets = new ArrayList<>();
        String sql = "SELECT * FROM pet WHERE petBreed = ?";
        try (Connection conn = baseDao.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, breed);
            try (ResultSet rs = pstmt.executeQuery()) {
                while (rs.next()) {
                    Pet pet = new Pet();
                    pet.setPetId(rs.getLong("petId"));
                    pet.setPetName(rs.getString("petName"));
                    pet.setPetBreed(rs.getInt("petBreed"));
                    pet.setPetSex(rs.getInt("petSex"));
                    pet.setBirthday(rs.getTimestamp("birthday"));
                    pet.setDescription(rs.getString("description"));
                    pets.add(pet);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return pets;
    }

    @Override
    public void addPet(Pet pet) {
        String sql = "INSERT INTO pet (petName, petBreed, petSex, birthday, description) VALUES (?, ?, ?, ?, ?)";
        try (Connection conn = baseDao.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, pet.getPetName());
            pstmt.setInt(2, pet.getPetBreed());
            pstmt.setInt(3, pet.getPetSex());
            pstmt.setTimestamp(4, new java.sql.Timestamp(pet.getBirthday().getTime()));
            pstmt.setString(5, pet.getDescription());
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean isPetNameUnique(String petName) {
        String sql = "SELECT COUNT(*) FROM pet WHERE petName = ?";
        try (Connection conn = baseDao.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, petName);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    return rs.getInt(1) == 0;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}
